package com.nulldev.util.io.lzma;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.nulldev.util.io.IOUtils;
import com.nulldev.util.io.lzma.lzma.Decoder;

public class LZMACompressor {
	private File rawFilePath;
    private File compressedFilePath;

    public LZMACompressor(File rawFilePath, File compressedFilePath) {
        this.rawFilePath = rawFilePath;
        this.compressedFilePath = compressedFilePath;
    }

    public void compress() {
        try {
        	LZMAOutputStream outputStream = new LZMAOutputStream.Builder(
                    new BufferedOutputStream(new FileOutputStream(compressedFilePath)))
                    .useMaximalDictionarySize()
                    .useMaximalFastBytes()
                    .build();
             InputStream inputStream = new BufferedInputStream(new FileInputStream(rawFilePath));
             IOUtils.copy(inputStream, outputStream);
        }catch (Exception e) {
        	e.printStackTrace();
        }
    }

    public void decompress() {
        try {
        	LZMAInputStream inputStream = new LZMAInputStream(
                    new BufferedInputStream(new FileInputStream(compressedFilePath)),
                    new Decoder());
            OutputStream outputStream = new BufferedOutputStream(
                         new FileOutputStream(rawFilePath));
            IOUtils.copy(inputStream, outputStream);
        }catch (Exception e) {
        	e.printStackTrace();
        }
    }
}
